<?php
class ControllerAccountWxLogin extends Controller {
    private $error = array();
    
    
   
    
    public function index()
    {
        if($this->customer->isLogged() && isset($this->session->data['wx_openid']))
        {
            $customer_info = $this->customer->getCustomerInfo();
            $this->response->setDataResult(1,"",$customer_info);
            return;
        }
        else if(isset($this->request->get['code']))
        {
            
            
            $openid_url = 'https://api.weixin.qq.com/sns/oauth2/access_token';
            $openid_request = "appid=wx1f7bb6bf5becd635&secret=252fc4cf0483e3df5389079f6d9e43cb&code=".$this->request->get['code']."&grant_type=authorization_code";
            
            $curl = curl_init();
            
            curl_setopt($curl, CURLOPT_URL, $openid_url);
            
            curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE); // 对认证证书来源的检查
            
            curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE); // 从证书中检查SSL加密算法是否存在
            
            curl_setopt($curl, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']); // 模拟用户使用的浏览器
            
            curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1); // 使用自动跳转
            
            curl_setopt($curl, CURLOPT_AUTOREFERER, 1); // 自动设置Referer
            
            curl_setopt($curl, CURLOPT_POST, 1); // 发送一个常规的Post请求
            
            curl_setopt($curl, CURLOPT_POSTFIELDS, $openid_request); // Post提交的数据包
            
            curl_setopt($curl, CURLOPT_TIMEOUT, 30); // 设置超时限制防止死循环
            
            curl_setopt($curl, CURLOPT_HEADER, 0); // 显示返回的Header区域内容
            
            curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); // 获取的信息以文件流的形式返回
            
            
            $openid_response = curl_exec($curl);
            
            
            curl_close($curl);
            
            $result = json_decode($openid_response);
            
            if(isset($result->unionid))
            {
                $this->session->data['wx_unionid'] = $result->unionid;
                $this->session->data['wx_openid'] = $result->openid;
                $this->load->model('account/social');
                $customer_id = $this->model_account_social->getBindCustomerIdByUid($result->unionid);
                if($customer_id)
                {
                    if ($this->customer->loginById($customer_id)) {
                        $this->load->model('account/activity');
                         
                        $activity_data = array(
                            'customer_id' => $this->customer->getId(),
                            'name'        => $this->customer->getFirstName()
                        );
                         
                        $this->model_account_activity->addActivity('login', $activity_data);
                         
                        $customer_info = $this->customer->getCustomerInfo();
                         
                        $this->response->setDataResult(1,"",$customer_info);
                        return;
                    }else 
                    {
                        $this->response->setDataResult(2,"",null);
                    }
                }
                else 
                {
                    $this->response->setDataResult(2,"",null);
                }
            }
            else
            {
                $this->response->setDataResult(0,"",$openid_response);
            }
        }
        else 
        {
            $this->response->setDataResult(0,"",null);
        }
        
    }
    
}